package com.study.blogsearchconsumer.controller;

import com.study.blogsearchconsumer.service.SearchFeignService;
import com.study.blogsearchpojo.entity.Blog;
import com.study.blogsearchpojo.entity.BlogBatchDto;
import com.study.blogsearchpojo.entity.BlogOneDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SearchFeignController {
    @Autowired
    private SearchFeignService searchFeignService;
    /**
     * 判断索引库是否已经存在
     */
    @PostMapping("/exists")
    @ResponseBody
    boolean exists(String indexName){
        return searchFeignService.exists(indexName);
    }

    /**
     * 删除索引库
     */
    @PostMapping("/dropIndex")
    @ResponseBody
    boolean dropIndex(String indexName){
        return searchFeignService.dropIndex(indexName);
    }

    /**
     * 创建博客索引库（标题、内容、作者、博客id） （标题、内容、作者可以分词检索）
     */
    @PostMapping("/createBlogIndex")
    @ResponseBody
    boolean createBlogIndex(String indexName){
        return searchFeignService.createBlogIndex(indexName);
    }


    /**
     * 录入一条博客
     */
    @PostMapping("/saveOne")
    @ResponseBody
    boolean saveOne(@RequestBody BlogOneDto blogOneDto){
        boolean saveOne = searchFeignService.saveOne(blogOneDto);
        return saveOne;
    }
    /**
     * 批量录入博客
     */
    @PostMapping("/addBlogsInBatches")
    @ResponseBody
    boolean addBlogsInBatches(@RequestBody BlogBatchDto blogBatchDto){
        return searchFeignService.addBlogsInBatches(blogBatchDto);
    }
    /**
     * 判断一条是否存在
     */
    @PostMapping("/existsOne")
    @ResponseBody
    boolean existsOne(String indexName, String id){
        return searchFeignService.existsOne(indexName, id);
    }
    /**
     * 删除一条
     */
    @PostMapping("/deleteOne")
    @ResponseBody
    boolean deleteOne(String indexName, String id){
        return searchFeignService.deleteOne(indexName, id);
    }
    /**
     * 更新一条博客
     */
    @PostMapping("/updateOne")
    @ResponseBody
    boolean updateOne(@RequestBody BlogOneDto blogOneDto){
        boolean flag = searchFeignService.updateOne(blogOneDto);
        return flag;
    }

    /**
     * 根据关键词  将作者(全匹配)、内容（模糊短语匹配）与搜索词相关的博客检索出来
     */
    @PostMapping("/search")
    @ResponseBody
    List<Blog> search(@RequestParam(value = "indexName") String indexName, @RequestParam(value ="keyword" ) String keyword,@RequestParam(value="pageNo")int pageNo){
        List<Blog> blogList = searchFeignService.search(indexName,keyword,pageNo);
        return blogList;
    }
//    /**
//     * 根据关键词  将标题联想出来
//     */
//    @PostMapping("/suggest")
//    @ResponseBody
//    List<String> suggest(@RequestParam(value = "indexName") String indexName, @RequestParam(value ="keyword" ) String keyword){
//        List<String> titleList = searchFeignService.suggest(indexName,keyword);
//        return titleList;
//    }
}
